// pages/coldManage/index.js
const dayjs = require("dayjs");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    townList: [ ], // 乡镇列表
    statusList: [  // 设备安装状态
      { text: '是否安装设备', value: '' },
      { text: '已安装', value: '0' },
      { text: '未安装', value: '1' },
    ],
    town: '',
    status: '',

    pullFlag: false, 
    totalPage: 0, // 总页数
    queryParams: {
      page: 1,
      size: 10,
    },
    tableList: [],
    listTip: '上拉加载更多',

    townList: [], // 乡镇列表
  },

  onShow () {
    // 获取乡镇
    wx.$req({
      url: '/wms/coldStorage/dept/list',
      method: 'post',
    }).then(res => {
      debugger
      // 只要镇一级的数据， 同时需要转为 text value  格式
      let list = res.data
      list = list.map(item => {
        return {
          text:item.deptName,
          value: item.deptId + '' // 如果是数字，强制转字符串
        }
      })

      // 增加一个前置的空状态
      list.unshift( { text: '请选择乡镇', value: '' } )
      this.setData({ townList: list })
    })
  },

  onLoad (options) {
    const {town, status} = options
    this.setData({
      status,
      town
    })

    this.getList()
  },

  townChange (val) {
    this.setData({ town: val.detail })
    this.reset()
    this.getList()
  },

  statusChange (val) {
    this.setData({ status: val.detail })
    this.reset()
    this.getList()
  },
 
  // 表单重置
  reset () {
    const page = 1
    const size = 10
    this.setData({
      pullFlag: false,
      queryParams: {page, size }
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.reset()
    this.getList()
  },

 /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

    let { page, size } = this.data.queryParams

    console.log(page, this.data.totalPage)

    if (page < this.data.totalPage) {

      page += 1
      this.setData({  
        pullFlag: true,
        queryParams: { page,  size },
      })
      this.getList()
    }
  },

  // 列表请求
  getList() {
    const {page, size} = this.data.queryParams
    wx.$req({
      url: `/wms/coldStorage/publishListForApp?page=${page - 1}&size=${size}`,
      method: 'post',
      data: {
        isInstall: this.data.status, // 设备安装情况 0是 1 否
        deptId: this.data.town, // 乡镇ID
      },
      loadingText: '加载中'
    }).then(res => {
      
      let rows = this.data.tableList
      let totalPage = res.data.totalPages
 
      // 时间格式化
      res.data.content.forEach(item => {
        item.publishTime = dayjs(item.publishTime).format('YYYY-MM-DD HH:ss:mm')
      })

      if (totalPage == 1 || this.data.totalPage === totalPage ) {
        this.setData({ listTip: '暂无更多' })
      } else {
        this.setData({ listTip: '上拉加载更多' })
      }

      if (totalPage == 0) {
        this.setData({ listTip: '暂无内容' })
      }


      // 列表数据：为true 则需要push, false，则直接覆盖
      if (this.data.pullFlag) {
        rows.push(...res.data.content)
      } else {
        rows = res.data.content
      }

      this.setData({ 
        tableList: rows,
        totalPage,
      })
    }).finally(() => { wx.stopPullDownRefresh() })
  },
 
  toDetail (e) {

    const info = JSON.stringify(e.currentTarget.dataset.info)

    wx.navigateTo({
      url: '/pages/leaseDetail/index',
      success: function(res) {
        res.eventChannel.emit('info', { data: info })
      }
    })
  }

})